<?php
/**
 * 店主服务
 * 
 * @author river_he@eueang.com
 */
include_once(AMFPHP_BASE . "shared/util/MethodTable.php");
final class ShoperService extends Kit_Service{
	/**
	 * 登陆
	 * 
	 * @param string $token	令牌
	 * @return array
	 */
	public function login($token){
		if($token == ''){
			return $this->pack(101, 'arrguments token error');
		}
		$return = array();
		$login = false; //$this->getMemcache()->get($token);
		$shopKey = 'shoper_';
		if(($login === false)){
			$where = "sToken='".$token."'";
			$shoper = $this->getShoperMod()->getItem($where);
			if($shoper){
				if(1 > $shoper['nShops']){
					$return['shoper'] = $shoper;
					$return['shops'] = 0;
					return $this->pack(200, 'OK', $return);
				}
				$shopKey .= $shoper['id'];
				$shops = $this->getShopMod()->getItems('nUid='.$shoper['id']);
				$return['shoper'] = $shoper;
				$return['shops'] = $shops;
				//$this->getMemcache()->set($token, array('uid'=>$shoper['id'], 'token'=>$token), 60);
				//$this->getMemcache()->set($shopKey, $return, 61);
			}else{
				return $this->pack(201, 'no this shoper '.$token);
			}
		}else{
			$shopKey .= $login['uid'];
			//$return = $this->getMemcache()->get($shopKey);
		}
		return $this->pack(200, 'OK', $return);
	}
	
	/**
	 * 获取我的道具列表
	 * 
	 * @param int $shoperId
	 * @param int $status
	 * @param int $page
	 * @param int $psize
	 * @return array
	 */
	public function getProps($shoperId, $status, $page, $psize){
		if(1 > $shoperId){
			return $this->pack(101, 'argument shoperId error');
		}
		try{
			$page = $page>0?$page:1;
			$psize = $psize>0?$psize:10;
			$data = $this->getShoperMod()->getProps($shoperId, $status, $page, $psize);
			return $this->pack(200, 'OK', $data);
		}catch(Exception $e){
			return $this->pack($e->getCode(), $e->getMessage());
		}
	}
	
	/**
	 * 使用道具
	 * 
	 * @param int $shoperId 店主id
	 * @param int $shopId 店id
	 * @param int $shopPropId 店主道具记录id
	 * @return array
	 */
	public function useProp($shoperId, $shopId, $shopPropId){
		if(1 > $shoperId){
			return $this->pack(101, 'argument shoperId error');
		}elseif(1>$shopId){
			return $this->pack(102, 'argument shopId error');
		}elseif(1 > $shopPropId){
			return $this->pack(103, 'argument shopPropId error');
		}
		$used = $this->getShoperMod()->useProp($shoperId, $shopId, $shopPropId);
		if(is_array($used)){
			return $this->pack(200, 'OK', $used);
		}else{
			$code = 200;
			$message = '';
			switch($used){
				case -1:
					$code += 1;
					$message .= '您没有获得该道具';
					break;
				case -2:
					$code += 2;
					$message .= '该道具您已经使用完';
					break;
				case -3:
					$code += 3;
					$message .= '店主和道具关系错误';
					break;
				case -4:
					$code += 4;
					$message .= '该道具不存在';
					break;
				case -5:
					$code += 5;
					$message .= '美食店不存在';
					break;
				case -6:
					$code += 6;
					$message .= '店主和美食店关系错误';
					break;
				case -7:
					$code += 7;
					$message .= '服务器忙';
					break;
				case -8:
					$code += 8;
					$message .= '服务器数据处理错误';
					break;
			}
			return $this->pack($code, $message);
		}
	}
	
	/**
	 * 使用装修材料
	 * 
	 * @param int $shoperId
	 * @param int $shopId
	 * @param int $decoId
	 * @param int $decoId
	 * @param int $x
	 * @param int $y
	 * @return array
	 */
	public function useDeco($shoperId, $shopId, $shoperDecoId, $x, $y){
		if(1 > $shoperId){
			return $this->pack(101, 'argument shoperId error');
		}elseif(1>$shopId){
			return $this->pack(102, 'argument shopId error');
		}elseif(1 > $shoperDecoId){
			return $this->pack(103, 'argument shoperDecoId error');
		}
		$used = $this->getShoperMod()->useDeco($shoperId, $shopId, $shoperDecoId, $x, $y);
		if(is_array($used)){
			return $this->pack(200, 'OK', $used);
		}else{
			$code = 200;
			$message = '';
			switch ($used){
				case -1:
					$code += 1;
					$message .= '您没有购买该装修材料';
					break;
				case -2:
					$code += 2;
					$message .= '该材料您已用完';
					break;
				case -3:
					$code += 3;
					$message .= '参数不一致';
					break;
				case -4:
					$code += 4;
					$message .= '该装修材料不存在';
					break;
				case -5:
					$code += 5;
					$message .= '店铺不促在';
					break;
				case -6:
					$code += 6;
					$message .= '参数不一致';
					break;
				case -7:
					$code += 7;
					$message .= '服务器忙请重试';
					break;
				case -8:
					$code += 8;
					$message .= '服务器处理错误';
					break;
			}
			return $this->pack($code, $message);
		}
	}
	
	/**
	 * 获取我的装修材料
	 * 
	 * @param int $shoperId 店主id
	 * @param int $status 0.未使用+使用中1.未使用2.使用中3.过期4.退货
	 * @param int $page 
	 * @param int $psize
	 * @return array
	 */
	public function getDecos($shoperId, $status, $page, $psize){
		if(1 > $shoperId){
			return $this->pack(101, 'argument shoperId error');
		}
		try{
			$page = $page>0?$page:1;
			$psize = $psize>0?$psize:10;
			$data = $this->getShoperMod()->getDecos($shoperId, $status, $page, $psize);
			return $this->pack(200, 'OK', $data);
		}catch(Exception $e){
			return $this->pack($e->getCode(), $e->getMessage());
		}
	}
	
	/**
	 * 获取好友列表
	 * 
	 * @param int $shoperId 店主id
	 * @param int $type 排序方式（0综合1蛋糕2冰欺凌3汉堡）等级降序
	 * @param int $page 页码
	 * @param int $psize 页记录数
	 * @return array
	 */
	public function getFriends($shoperId, $type, $page, $psize){
		try{
			$page = $page>0?$page:1;
			$psize = $psize>0?$psize:10;
			$data = $this->getShoperMod()->getFriends($shoperId, $type, $page, $psize);
			return $this->pack(200, 'OK', $data);
		}catch(Exception $e){
			return $this->pack($e->getCode(), $e->getMessage());
		}
	}
}
//end